package source.javadog;

import cn.hutool.core.util.RandomUtil;
import org.redkale.source.DataJdbcSource;
import org.redkale.source.DataSource;
import org.redkale.source.DataSources;
import source.javadog.bean.Person;
import source.javadog.mysource.MyDataJdbcSource;
import source.javadog.mysource.MyDataSource;
import source.javadog.mysource.MyDataSources;

import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * Author : manyu
 * Date: 2018/9/9 11:22
 * Description: 启动类
 */
public class Application {

    public static void main3() throws IOException {
        URL resource = MyDataJdbcSource.class.getClassLoader().getResource("db.properties");
        MyDataSource dataSource = MyDataSources.createDataSource("dburl", resource);
        //插入
        int pk = RandomUtil.randomInt();
        Person insert = new Person().setId(pk).setAge(10).setName(RandomUtil.randomString(5));
        dataSource.save(insert);
        System.out.println("插入" + insert);
        //修改
        insert.setAge(RandomUtil.randomInt());
        dataSource.update(insert);
        //根据主键查
        Person person = dataSource.find(Person.class, pk);
        System.out.println("更改后" + person);
        //根据主键删除
        dataSource.delete(Person.class,pk);
        //查列表
        List<Person> personList = dataSource.findList(Person.class);
        //根据主键查
        Person delperson = dataSource.find(Person.class, pk);
        System.out.println("删除后" + delperson);
    }

    public static void main2() throws IOException {
        URL resource = DataJdbcSource.class.getClassLoader().getResource("persistence.xml");
        DataSource dataSource = DataSources.createDataSource(null, resource);
        List<Person> personList = dataSource.queryList(Person.class);
        //插入
        Person insert = new Person().setId(RandomUtil.randomInt()).setAge(10).setName(RandomUtil.randomString(5));
        dataSource.insert(insert);

        System.out.println(personList);
    }


    public static void main(String[] args) throws Exception {
        main3();
    }
}
